<?php
/*
*All rights reserved: Json.
*Links:http://www.liyaoquan.cn.
*Links:http://imarkchina.cn.
*Links:http://www.imarkchina.cn.
*/

/**
 * 博客前台评论模型，负责数据，增，删，改，查
 * 因为没有博客数据没使用数据库，所以没有继承 Model 类
 */
Class PinglunModel extends PublicModel{

	/**
	 * 显示评论
	 * @param  [type] $id [接收ID]
	 * @return [type]     [description]
	 */
	Public function getPunlunData($id){
		$C = $this->blogConfig();
		$data = loadfile(C('pinglun.id').$id.'.php');
		$indexpage = $C['pinglun_page'];
		$count = count($data);
		$lastpage = ceil($count / $indexpage);
		$pageurl = U('show/index', array('id' => $id), true);
		$page = new Page($count, $indexpage, $pageurl);
		$pnum = explode(',', $page->limit());
		$pageall = $page->getAll();
		$firpage = (isset($pageall['first']['page'])) ? $pageall['first']['page'] : '';
		$uppage = (isset($pageall['prev']['page'])) ? $pageall['prev']['page'] : '';
		$nepage = (isset($pageall['next']['page'])) ? $pageall['next']['page'] : '';
		$laspage = (isset($pageall['last']['page'])) ? $pageall['last']['page'] : '';
		$current = (isset($pageall['current'])) ? $pageall['current'] : '';
		$pagearr = str_replace(C('URLFIX'), C('URLFIX').'#comments', $page->view());
		$temp = array();
		foreach ($data as $k => $v) {
			$temp[] = unfile(C('pinglun.data').$k.'.Mark');
		}
		$array = PinglunLeve::getPL($temp);
		foreach ($array as $k => $v) {
			$array[$k]['reusername'] = PinglunLeve::getUsername($array, $v['pid']);
		}
		$lastarr = array();
		for ($i=0; $i < count($array); $i++) {
			if ($i < $pnum[0] || $i >= $pnum[0] + $pnum[1]) continue;
			$lastarr[] = $array[$i];
		}
		foreach ($lastarr as $k => $v) {
			$lastarr[$k]['content'] = $this->msgReplace($v['content']);
			if (!empty($v['user_id'])) {
				$user = loadfile(C('user').$v['user_id'].'.php');
				if ($user) {
					$lastarr[$k]['username'] = $user['username'];
					$lastarr[$k]['usermsg'] = $user['msg'];
					$lastarr[$k]['url'] = $user['url'];
					$lastarr[$k]['useremail'] = $user['email'];
				}
			}
			if ($v['username'] == '@i@Mark@China@') {
				$config = $this->blogConfig();
				$lastarr[$k]['username'] = $config['user_nick'];
			}
		}
		$pages['firpage'] = (int)$firpage;
		$pages['uppage'] = (int)$uppage;
		$pages['nepage'] = (int)$nepage;
		$pages['laspage'] = (int)$laspage;
		$pages['current'] = (int)$current;
		$redata['lastpage'] = (int)$lastpage;
		$redata['data'] = $lastarr;
		$redata['page'] = $pages;
		return $redata;
	}

	/**
	 * 保存评论操作
	 * @param  [type] $arr [接收数组]
	 * @return [type]      [description]
	 */
	Public function savepunlun($array){
		if (Badword::word($array['username']) == '*' || Badword::word($array['content']) == '*') {
			return false;
		}
		unset($array['code']);
		$file = C('pinglun.id').$array['id'].'.php';
		$fid = loadfile($file);
		$dir = C('pinglun.data');
		$filename = randCode(6, 0);
		$day = date('Y.m.d');
		$time = date('H:i:s', time());
		if (file_exists($dir.$filename.'.Mark')) {
			$filename = randCode(6, 0);
		}
		$array['rid'] = count(GetFileList($dir)) + 1;
		$array['date'] = $day;
		$array['time'] = $time;
		$array['fileid'] = $filename;
		$put = array(
			'date' => $day,
			'time' => $time,
			);
		$fid[$filename] = $put;
		uasort($fid, 'Post_Sort');
		if ($array['pid']) {
			$config = $this->blogConfig();
			if ($config['smtp_PL'] == 'open') {
				$findcheck = $this->getPunlunData($array['id']);
				$getdata = PinglunLeve::pinglunreemail($findcheck['data'], $array['pid']);
				$getdata['reurl'] = U('index/show/index', array('id' => $array['id']));
				$getdata['content'] = $this->msgReplace($array['content']);
				$getdata['blogtitle'] = str_replace('{blogtitle}', $config['site_name'], $config['smtp_title']);
				$getdata['mailcontetn'] = str_replace(array('{day}', '{time}' ,'{username}' ,'{content}' ,'{blogurl}', '{blogtitle}'), array($day, $time, $array['username'], $getdata['content'], $getdata['reurl'], $config['site_name']), $config['smtp_code']);
				$Edata['host'] = $config['smtp_server'];
				$Edata['username'] = $config['smtp_username'];
				$Edata['password'] = $config['smtp_pass'];
				$Edata['from'] = $config['smtp_forme'];
				$Edata['fromname'] = $config['smtp_nick'];
				$Edata['host'] = $config['smtp_server'];
				$Edata['host'] = $config['smtp_server'];
				$Edata['host'] = $config['smtp_server'];
				$Edata['rename'] = $getdata['rename'];
				$Edata['remail'] = $getdata['reemail'];
				$Edata['blogtitle'] = $getdata['blogtitle'];
				$Edata['content'] = $getdata['mailcontetn'];
				$this->ReEmail($Edata, true);
			}
		}
		if (varfile($file, $fid) && serfile($dir.$filename.'.Mark', $array)) {
			return true;
		}
	}
}